#from ETP.models import Step
from selenium import webdriver
import time
from .UIError import UIError
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from ETP.models import Step



class SeleniumUtils:

    DRIVER_FOLDER = '../../bin/'

    def OpenBrowser(self,browserType,urlPath):
        self.url = urlPath
        self.browser = browserType

        if browserType == 'Chrome':
            self.driver = webdriver.Chrome(executable_path=self.DRIVER_FOLDER + 'chromedriver.exe')
            self.driver.get(self.url)
        else:
            raise UIError('目前只支持Chrome浏览器，敬请期待后续版本')

    def FindElement(self,locate_method,locate_url):
        self.locate_method = locate_method
        self.locate_url = locate_url

        if self.locate_method == 'ID':
            try:
                WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(By.ID,self.locate_url))
                self.Element = self.driver.find_element_by_id(self.locate_url)
                return self.Element
            except:
                raise UIError('定位的元素没有出现，查看元素是否编写正确')
        elif self.locate_method == 'NAME':
            try:
                WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(By.NAME, self.locate_url))
                self.Element = self.driver.find_element_by_name(self.locate_url)
                return self.Element
            except:
                raise UIError('定位的元素没有出现，查看元素是否编写正确')
        elif self.locate_method == 'XPATH':
            try:
                WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(By.XPATH, self.locate_url))
                self.Element = self.driver.find_element_by_xpath(self.locate_url)
                return self.Element
            except:
                raise UIError('定位的元素没有出现，查看元素是否编写正确')
        else:
            return None

    def Click(self,locate_method,locate_url):
        self.locate_method = locate_method
        self.locate_url = locate_url
        try:
            self.Element = self.FindElement(self.locate_method,self.locate_url)
            self.Element.click()
        except:
            raise UIError('点击元素失败，查看页面加载或定位url')




    def Type(self,locate_method,locate_url,data):
        self.locate_method = locate_method
        self.locate_url = locate_url
        self.data = data
        try:
            self.Element = self.FindElement(self.locate_method,self.locate_url)
            self.Element.send_keys(self.data)
        except:
            raise UIError('输入元素失败，查看页面加载或定位url')


    def Check(self,locate_method,locate_url,data):
        self.locate_method = locate_method
        self.locate_url = locate_url
        self.data = data
        try:
            self.Element = self.FindElement(self.locate_method,self.locate_url)
            UIText = self.Element.text
        except:
            raise UIError('检查元素失败，查看页面加载或定位url')
        try:
            assert UIText.strip() == self.data
        except:
            raise UIError('数据比对出现异常，请查数据是否一致')



    def CloseBrowser(self):
        self.driver.close()


    '''
        通过step的Id运行step
    '''
    def run_ui(self,step_id):
        #通过stepID获取step信息
        self.stepID = step_id
        self.step = Step.objects.get(id = self.stepID)
        self.ui_operation = self.step.ui_operation
        self.ui_locateMethod = self.step.ui_locate_method
        self.ui_locateUrl = self.step.ui_locate_url
        self.ui_data = self.step.ui_data
        if hasattr(self,self.ui_operation):
            func = getattr(self,self.ui_operation)
        else:
            raise UIError('关键字不存在，请验证后在使用')

        #通过step的UIoperation来反射确定使用的方法

if __name__ == '__main__':
    sss = SeleniumUtils()
    sss.OpenBrowser('Chrome','http://www.baidu.com')
    time.sleep(30)
    sss.CloseBrowser()